package com.ericsson.resourceplanner.dao.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NamedQuery;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ericsson.resourceplanner.dao.AdminDao;
import com.ericsson.resourceplanner.dao.entity.EmployeeProjectDailyBackup;
import com.ericsson.resourceplanner.dao.entity.EriproDumpDetail;
import com.ericsson.resourceplanner.dao.entity.Report;
import com.ericsson.resourceplanner.dto.EriproDto;

@Repository
public class AdminDaoImpl implements AdminDao{
	@Autowired
	private EntityManagerFactory entityManagerFactory;

	protected Logger logger = LoggerFactory.getLogger(getClass());
	@PersistenceContext
	EntityManager entityManager;
	
	static int i = 0;
	
	EriproDumpDetail edump = null;
	@Override
	@Transactional
	public void uploadFileInDB(Set<EriproDto> edtoset)
	{
		
		//System.out.println("uploadFileInDB in AdminDaoImpl .... Set size="+edtoset.size());
		//EntityManager entityManager=emf.createEntityManager();
		
		logger.error("Within Dao Impl....-------------------------------------. Set size="+edtoset.size());

		Iterator<EriproDto> edtoit = edtoset.iterator();
		EriproDto edto = null;
		
		while(edtoit.hasNext())
		{
			edto = edtoit.next();
			
			edump = new EriproDumpDetail();
			edump.setCustomer(edto.getCustomer_name());
			//System.out.println("edump customer name===="+edto.getCustomer_name());
			edump.setDomain(edto.getDomain_name());
			edump.setEmpName(edto.getEmp_name());
			//System.out.println("edump customer name===="+edto.getEmp_name());
			edump.setEndDate(edto.getEnddate());
			edump.setFte(new BigDecimal(edto.getFte_hrs()));
			edump.setProjectId(new BigDecimal(edto.getProj_id()));
			edump.setProjectName(edto.getProj_name());
			edump.setRegion(edto.getRegion_code());
			edump.setSignumId(edto.getSignum_id());
			edump.setStartDate(edto.getStartdate());
			edump.setStatus(edto.getCurr_status());
		
			entityManager.persist(edump);

			//logger.debug("Persisting an object--------------------------"+i++);
			edump = null;
			edto = null;
			
		}
		entityManager.flush();
		logger.debug("50 Objects Persisted in DB.................................");
		entityManager.close();		
	}
	
	@Transactional
	public void insertProjRecords()
	{
		try
		{
			Query q = entityManager.createNativeQuery("call PROJ_INSERTDATA_PROC()") ;
			q.executeUpdate();
			//return q.getResultList();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			//return null;
		}
	}
	
	@Transactional
	public void insertEmpProjRecords()
	{
		try
		{
			Query q = entityManager.createNativeQuery("call EMP_PROJ_INSERTDATA_PROC()");
			q.executeUpdate();
			//return q.getResultList();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			//return null;
		}
	}
	@Override
	public List<Report> getExportResultsFromDB(Date startDate, Date endDate) {
		List<Report> reportDetails=null;
		EntityManager em=entityManagerFactory.createEntityManager();
		Query query=em.createNamedQuery("getReportForDates");
		query.setHint("org.hibernate.fetchSize", 50);
		query.setParameter("startDate", startDate);
		query.setParameter("endDate", endDate);
		reportDetails= (List<Report>)query.getResultList();
		em.close();
		return reportDetails;
		
	}
	
	@Override
	public void saveprojdailyutilization(EmployeeProjectDailyBackup obj) {
		
		entityManager.persist(obj);
		
		 
		
	}
	
	@Override
	public String getbatch_id()
	{
		String qlString = "SELECT nvl(max(x.batch_id),0)+1 FROM EmployeeProjectDailyBackup x";
		Query q = entityManager.createQuery(qlString);
		Integer results = (Integer) q.getSingleResult();			
		return results.toString();
	}
	
	@Override
	public void populate_week_data()
	{
		Query q = entityManager.createNativeQuery("call WEEK_UTILIZATION_PROC()");
		q.executeUpdate();		

	}

	
	
}
